<?php

/**
 * Controller_score
 * 学生成绩
 */

class Controller_score extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->class    = YOU::loadModel('class');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->student  = YOU::loadModel('student');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->semester = YOU::loadModel('semester');
		$this->model->exam     = YOU::loadModel('exam');
		$this->model->score    = YOU::loadModel('score');
		$this->model->excel    = YOU::loadModel('excel');
	}

	/** 首页 **/
	public function actionIndex()
	{

	}


	/** 统计科目设置 **/
	public function actionSubject()
	{
		$class_id = $this->get->class_id;

		//  读出设置
		$set = $this->model->score->getSubject($class_id);
		// 综合科目列表

		// 学科列表
		//$subject_lists = $this->model->subject->getAll();
		$row = $this->db->fetchRow("SELECT * FROM `tbl_class_up` WHERE class_id = '{$class_id}'");
		$subjects = unserialize($row['class_subject']);
		$subject_lists = array();
		if(is_array($subjects)){
			foreach ($subjects as $key => $value){
				$subject_lists[$key]['subject_id'] = $key;
				$subject_lists[$key]['subject_name'] = $value;
			}
		}

		if ($_POST){

			if (empty($class_id)){
				Helper_Client::alert("请选择班级");
			}

			$score_total = serialize($this->post->score_total);
			$to_class = $this->post->to_class;

			foreach ($subject_lists as $key => $value){
				$score_order1[$value['subject_id']] = $_POST["score_order_{$value['subject_id']}"];
			}
			foreach ($set['score_group'] as $key => $value){
				$score_order2[$key] = $_POST["score_order_{$key}"];
			}
			$score_order = array($score_order1,$score_order2);
			$score_order = serialize($score_order);
			$info = array(
			"score_order" => $score_order,
			"score_total" => $score_total,
			);

			// 保存
			$this->model->score->setsubject($class_id, $info);
			// 复制给其他班级
			if (!empty($to_class)){
				foreach ($to_class as $key => $value){
					if (substr($value, 0, 1) == "c"){
						$class_lists[] = substr($value, 1);
					}
				}
			}
			$this->model->score->copysubject($class_id, $class_lists);

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=score.subject&class_id={$class_id}");

		}

		foreach ($subject_lists as $key => $value){

			// 报表顺序
			if ($set['score_order'][0][$value['subject_id']]){
				$subject_lists[$key]['score_order'] = $set['score_order'][0][$value['subject_id']];
			}
			// 总分选中
			if (@in_array($value['subject_id'], $set['score_total'])){
				$subject_lists[$key]['is_total'] = 1;
			}
		}


		// 班级列表tree
		$class_lists = $this->model->class->getTree();

		// 综合科目显示
		if (!empty($set['score_group'])){
			foreach ($set['score_group'] as $key => $value){
				$tmp = "";
				foreach ($value as $k => $v){
					$tmp[] = $this->model->subject->getNameById($v);
				}
				$set['score_group'][$key] = implode(",", $tmp);
			}
		}
		$group_order = $set['score_order'][1];

		// 班级信息
		$class_data = $this->model->class->getInfo($class_id);

		$this->tpl->assign("group_order", $group_order);
		$this->tpl->assign("set", $set);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->assign("class_lists", $class_lists);

		$this->tpl->assign("class_data", $class_data);
		$this->tpl->display('score/score.subject.tpl.php');
	}


	/** 分数段设置列表 **/
	public function actionRangeList()
	{
		$class_id = (int)$this->get->class_id;

		$where = " WHERE class_id='{$class_id}' ";

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_score_range` {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		$range_lists = $this->db->fetchAll("SELECT * FROM `tbl_score_range` {$where} ORDER BY range_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($range_lists as $key => $value){
			$exam_data = $this->model->exam->getInfo($value['exam_id']);
			$range_lists[$key]['exam_type']     = $this->exam_type_ini[$exam_data['exam_type']];
			$range_lists[$key]['exam_name']     = $exam_data['exam_name'];
			$range_lists[$key]['subject_name']  = $this->model->score->getRangeSubject($value['range_id']);
			$range_lists[$key]['range_level']   = unserialize($value['range_level']);
			$range_lists[$key]['range_section'] = unserialize($value['range_section']);
		}


		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));

		// 班级列表tree
		$class_lists = $this->model->class->getTree();

		// 班级信息
		$class_data = $this->model->class->getInfo($class_id);


		$this->tpl->assign("class_data", $class_data);
		$this->tpl->assign("class_lists", $class_lists);

		$this->tpl->assign("range_lists", $range_lists);
		$this->tpl->display('score/score.rangelist.tpl.php');
	}

	/** 分数段修改 **/
	public function actionRangeUpdate()
	{
		$range_id = (int)$this->get->range_id;
		$range_data = $this->model->score->getRangeInfo($range_id);

		if ($_POST){

			$range_full      = $this->post->range_full;
			$range_fullopen  = $this->post->range_fullopen;
			// 分阶部分
			foreach ($_POST as $key => $value){
				if (substr($key, 0, 10) == "level_name"){
					$ls = substr($key, 10);
					$level[$ls]['name'] = $value;
				}
				if (substr($key, 0, 9) == "level_max"){
					$ls = substr($key, 9);
					$level[$ls]['max'] = $value;
				}
				if (substr($key, 0, 9) == "level_min"){
					$ls = substr($key, 9);
					$level[$ls]['min'] = $value;
				}
			}
			$level = serialize($level);

			// 分数段部分
			foreach ($this->score_section_ini as $key => $value){
				$section[$key]['min']  = $_POST["sectionmin_{$key}"];
				$section[$key]['max']  = $_POST["sectionmax_{$key}"];
				$section[$key]['open'] = $_POST["sectionopen_{$key}"];
			}
			$section = serialize($section);

			$info = array(
			"range_level"    => $level,
			"range_full"     => $range_full,
			"range_fullopen" => $range_fullopen,
			"range_section"  => $section,
			"update_time"    => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_score_range", $info, "range_id='{$range_id}'");

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=score.rangelist&class_id={$range_data['class_id']}");
		}


		$exam_data = $this->model->exam->getInfo($range_data['exam_id']);
		$range_data['exam_type'] = $this->exam_type_ini[$exam_data['exam_type']];
		$range_data['exam_name'] = $exam_data['exam_name'];
		// 学科
		$range_data['score_subject'] = $this->model->score->getRangeSubject($range_id);

		$this->tpl->assign("range_data", $range_data);
		$this->tpl->display('score/score.rangeupdate.tpl.php');
	}

	/** 分数段删除 **/
	public function actionRangeDel()
	{
		$class_id = (int)$this->get->class_id;
		$range_id = (int)$this->get->range_id;
		$this->db->query("DELETE  FROM `tbl_score_range` WHERE range_id='{$range_id}'");
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=score.rangelist&class_id={$class_id}");
	}

	/** 分数段设置 **/
	public function actionRangeAdd()
	{
		$class_id = $this->get->class_id;

		if ($_POST){
			$subject_id      = $this->post->subject_id;
			$score_group     = $this->post->score_group;
			$score_end       = $this->post->score_end;
			$range_full      = $this->post->range_full;
			$range_fullopen  = $this->post->range_fullopen;
			$exam_ids        = $this->post->exam_ids;

			if (empty($exam_ids)){
				Helper_Client::alert("请选择考试");
			}

			if (empty($class_id)){
				Helper_Client::alert("请选择班级");
			}

			// 分阶部分
			foreach ($_POST as $key => $value){
				if (substr($key, 0, 10) == "level_name"){
					$ls = substr($key, 10);
					$level[$ls]['name'] = $value;
				}
				if (substr($key, 0, 9) == "level_max"){
					$ls = substr($key, 9);
					$level[$ls]['max'] = $value;
				}
				if (substr($key, 0, 9) == "level_min"){
					$ls = substr($key, 9);
					$level[$ls]['min'] = $value;
				}
			}
			$level = serialize($level);

			// 分数段部分
			foreach ($this->score_section_ini as $key => $value){
				$section[$key]['min']  = $_POST["sectionmin_{$key}"];
				$section[$key]['max']  = $_POST["sectionmax_{$key}"];
				$section[$key]['open'] = $_POST["sectionopen_{$key}"];
			}
			$section = serialize($section);

			$to_subject = $this->post->to_subject;
			// 复制给其他学科
			if (!empty($to_subject)){
				foreach ($to_subject as $key => $value){
					$subject_lists[] = $value;
				}
			}
			if (!empty($subject_id)){
				$subject_lists[] = $subject_id;
			}

			$to_class   = $this->post->to_class;
			// 复制给其他班级
			if (!empty($to_class)){
				foreach ($to_class as $key => $value){
					if (substr($value, 0, 1) == "c"){
						$class_lists[] = substr($value, 1);
					}
				}
			}
			$class_lists[] = $class_id;


			// 写入数据库
			foreach ($exam_ids as $e){
				foreach ($class_lists as $c){
					foreach ($subject_lists as $s){
						// 查询是否已存在
						$row = $this->db->fetchRow("SELECT * FROM `tbl_score_range` WHERE class_id='{$c}' AND exam_id='{$e}' AND subject_id='{$s}'");
						if (!empty($row)){
							$info = array(
							"range_level"    => $level,
							"range_full"     => $range_full,
							"range_fullopen" => $range_fullopen,
							"range_section"  => $section,
							"update_time"    => date("Y-m-d H:i:s"),
							);
							$this->db->do_update("tbl_score_range", $info, "range_id='{$row['range_id']}'");
						}else{

							$info = array(
							"class_id"       => $c,
							"exam_id"        => $e,
							"subject_id"     => $s,
							"score_group"    => $score_group,
							"score_end"      => $score_end,
							"range_level"    => $level,
							"range_full"     => $range_full,
							"range_fullopen" => $range_fullopen,
							"range_section"  => $section,
							"status"         => 1,
							"create_time"    => date("Y-m-d H:i:s"),
							"update_time"    => "0000-00-00 00:00:00",
							);
							$this->db->do_insert("tbl_score_range", $info);
						}
					}
				}
			}

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=score.rangelist&class_id={$class_id}");

		}

		// 班级信息
		$class_data = $this->model->class->getInfo($class_id);

		// 取得配置
		$set = $this->model->score->getSubject($class_id);
		$score_group = $set['score_group'];

		// 考试列表
		$tmp_lists = $this->model->exam->getAll($class_data['grade_id']);
		foreach ($tmp_lists as $key => $value){
			// 考试类型
			if (!empty($this->exam_type_ini[$value['exam_type']])){
				$exam_lists[$value['exam_type']]['name'] = $this->exam_type_ini[$value['exam_type']];
				$exam_lists[$value['exam_type']]['exam'][$value['exam_id']] = $value['exam_name'];
			}
		}


		// 学科列表
		$subject_lists = $this->model->subject->getAll();

		// 班级列表tree
		$class_lists = $this->model->class->getTree();


		$this->tpl->assign("class_data", $class_data);
		$this->tpl->assign("score_group", $score_group);
		$this->tpl->assign("exam_lists", $exam_lists);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('score/score.rangeadd.tpl.php');
	}


	/** 添加综合科目 **/
	public function actionGroupAdd()
	{
		$class_id    = $this->post->class_id;
		$group_name  = $this->post->group_name;
		$score_group = $this->post->score_group;

		if (empty($class_id)){
			Helper_Client::alert("请选择班级");
		}

		// 取得配置
		$set = $this->model->score->getSubject($class_id);
		$group = $set['score_group'];
		$groups = $set['score_order'][1];
		//print_R($set);
		$group[$group_name] = $score_group;
		$groups[$group_name]='';
		$groups = array($set['score_order'][0],$groups);
				
		$info = array(
		"score_group" => serialize($group),
		"score_order" => serialize($groups),
		);
		
		$this->model->score->setSubject($class_id, $info);

		// 跳转
		Helper_Client::reurl("index.php?a=score.subject&class_id={$class_id}");
	}

	/** 删除综合科目 **/
	public function actionGroupDel()
	{
		$class_id    = (int)$this->get->class_id;
		$group_name  = $this->get->group_name;
		if (empty($class_id)){
			Helper_Client::alert("请选择班级");
		}

		// 取得配置
		$set = $this->model->score->getSubject($class_id);
		$group = $set['score_group'];
		$groups = $set['score_order'][1];
		
		// 删除
		unset($group[$group_name]);
		unset($groups[$group_name]);
		$groups = array($set['score_order'][0],$groups);
		
		$info = array(
		"score_group" => serialize($group),
		"score_order" => serialize($groups),
		);

		$this->model->score->setSubject($class_id, $info);

		// 跳转
		Helper_Client::reurl("index.php?a=score.subject&class_id={$class_id}");


	}



	/** 成绩录入 **/
	public function actionAddStep()
	{
		// 考试列表
		$tmp_lists = $this->model->exam->getAll();
		foreach ($tmp_lists as $key => $value){
			// 考试类型
			$exam_lists[$value['exam_type']]['name'] = $this->exam_type_ini[$value['exam_type']];
			$exam_lists[$value['exam_type']]['exam'][$value['exam_id']] = $value['exam_name'];
		}

		// 班级列表 -- 任课的
		if ($this->user['user_type'] == "staff"){
			$class_lists = $this->db->fetchAll("SELECT a.class_id,b.class_name FROM `tbl_staff_subject` AS a,`tbl_class` AS b WHERE a.class_id=b.class_id AND a.staff_id='{$this->user['type_id']}'");

			//$class_lists = $this->model->class->getClassAllByStaff($this->user['type_id']);
		}

		// 学科
		//$subject_lists = $this->model->subject->getAll();
		if($this->user['user_type']=='staff'){
			$subject_lists = $this->db->fetchAll("SELECT b.* FROM tbl_staff_subject as a, tbl_subject as b WHERE a.subject_id = b.subject_id AND a.staff_id ='{$this->user['type_id']}' GROUP BY subject_id");
		}else{
			$subject_lists = array(array());
		}

		$this->tpl->assign("exam_lists", $exam_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->display("score/score.addstep.tpl.php");
	}

	/** 成绩录入 **/
	public function actionAdd()
	{
		$class_id   = $this->get->class_id;
		$subject_id = $this->get->subject_id;
		$exam_id    = $this->get->exam_id;

		// 班级信息
		$class_data = $this->model->class->getInfo($class_id);

		// 班级内所有学生
		$student_lists = $this->model->student->getInfoclass($class_id);

		if ($_POST){

			foreach ($student_lists as $key => $value){
				$student_id = $value['student_id'];
				$score = $_POST["score_{$student_id}"];

				$this->model->score->record($exam_id, $class_id, $student_id, $subject_id, $score);

			}

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=score.list&class_id={$class_id}&exam_id={$exam_id}&subject_id={$subject_id}");

		}

		$exam_name    = $this->model->exam->getNameById($exam_id);
		$class_name   = $this->model->class->getNameById($class_id);
		$subject_name = $this->model->subject->getNameById($subject_id);

		$this->tpl->assign("exam_name", $exam_name);
		$this->tpl->assign("class_name", $class_name);
		$this->tpl->assign("subject_name", $subject_name);
		$this->tpl->assign("student_lists", $student_lists);
		$this->tpl->display("score/score.add.tpl.php");
	}


	/** 分数段自定义查询 开始 **/
	public function actionDiyStep()
	{
		// 学期列表
		$semester_lists = $this->model->semester->getAll();

		// 年级列表
		$grade_lists = $this->model->grade->getAll();

		// 学科列表
		$subject_lists = $this->model->subject->getAll();

		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->display("score/score.diystep.tpl.php");
	}

	/** 分数段自定义查询 **/
	public function actionDiy()
	{
		$semester_id = (int)$this->get->semester_id;
		$exam_id     = (int)$this->get->exam_id;
		$grade_id    = (int)$this->get->grade_id;
		$subject_id  = (int)$this->get->subject_id;
		$class_ids   = $this->get->class_ids;

		// 分数段
		foreach ($_GET as $key => $value){
			if (substr($key, 0, 6) == "range_"){
				$tmp = explode("_", $key);
				$range[$tmp[2]][$tmp[1]] = $value;
			}
		}

		// 学科还是全部
		$where = "  ";
		if ($subject_id != 0){
			$where .= " AND subject_id='{$subject_id}' ";
			$subject_name = $this->model->subject->getNameById($subject_id);
		}else{
			$subject_name = "总分";
		}


		// 数据列表
		if (!empty($class_ids)){
			$class_lists = $this->model->class->getListByGrade($grade_id);
			foreach ($class_lists as $key => $value){
				// 年级下选中的班级才统计
				if (in_array($value['class_id'], $class_ids)){
					// 各班按学科分数段成绩分类
					foreach ($range as $k => $v){
						$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_score` WHERE exam_id='{$exam_id}'  AND class_id='{$value['class_id']}' AND score_value>='{$v['min']}' AND score_value<='{$v['max']}' {$where}");
						$data[$value['class_id']]['score'][$k] = (int)$row['count'];
					}

				}
				$data[$value['class_id']]['class_name'] = $value['class_name'];
			}
		}

		// 学期列表
		$semester_lists = $this->model->semester->getAll();

		// 年级列表
		$class_lists = $this->model->class->getTree();

		// 班级选中
		if (!empty($class_ids)){
			foreach ($class_lists as $key => $value){
				foreach ($value['class'] as $k => $v){
					if (in_array($v['class_id'], $class_ids)){
						$class_lists[$key]['class'][$k]['check'] = 1;
					}
				}
			}
		}


		// 学科列表
		$subject_lists = $this->model->subject->getAll();

		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("subject_lists", $subject_lists);

		$this->tpl->assign("grade_data", $this->model->grade->getInfo($grade_id));
		$this->tpl->assign("semester_data", $this->model->semester->getInfo($semester_id));
		$this->tpl->assign("exam_data", $this->model->exam->getInfo($exam_id));
		$this->tpl->assign("subject_name", $subject_name);
		$this->tpl->assign("subject_id", $subject_id);
		$this->tpl->assign("range", $range);
		$this->tpl->assign("class", $class);
		$this->tpl->assign("data_lists", $data);
		$this->tpl->display("score/score.diy.tpl.php");
	}



	/** 分数质量分析表格模板添加 **/
	public function actionTpAdd()
	{
		$grade_id = (int)$this->get->grade_id;
		$tp_type  = $this->get->tp_type;

		if (empty($grade_id)){
			Helper_Client::alert("请选择年级");
		}


		if ($_POST){
			if (empty($tp_type)){
				$tp_type = 1;
			}

			$subject_id = $this->post->subject_id;
			$exam_id    = $this->post->exam_id;
			$to_exam    = $this->post->to_exam;
			$to_subject = $this->post->to_subject;
			$to_grade   = $this->post->to_grade;


			foreach ($_POST as $key => $value){

				// 统计字段
				if (substr($key, 0, 5) == "stat_"){
					$stat[substr($key, 5)]['show'] = $value;
				}
				if (substr($key, 0, 10) == "statorder_"){
					$stat[substr($key, 10)]['order'] = $value;
					$order[$value] = $key;
				}
				if (substr($key, 0, 14) == "statrange_max_"){
					$stat[substr($key, 14)]['max'] = $value;
					$order[$value] = $key;
				}
				if (substr($key, 0, 14) == "statrange_min_"){
					$stat[substr($key, 14)]['min'] = $value;
					$order[$value] = $key;
				}

				// 分阶
				if (substr($key, 0, 11) == "range_name_"){
					$range[substr($key, 11)]['name'] = $value;
				}
				if (substr($key, 0, 10) == "range_max_"){
					$range[substr($key, 10)]['max'] = $value;
				}
				if (substr($key, 0, 10) == "range_min_"){
					$range[substr($key, 10)]['min'] = $value;
				}
				if (substr($key, 0, 11) == "range_rate_"){
					$range[substr($key, 11)]['rate'] = $value;
				}

				// 分析行
				if (substr($key, 0, 5) == "note_"){
					$note[substr($key, 5)] = $value;
				}
			}

			// 统计项目排序
			@array_multisort($order, SORT_ASC, $stat);

			$tp_stat  = serialize($stat);
			$tp_range = serialize($range);
			$tp_note  = serialize($note);


			// 复制到其他
			$to_exam[]    = $exam_id;
			$to_subject[] = $subject_id;
			$to_grade[]   = $grade_id;
			foreach ($to_exam as $e){
				foreach ($to_subject as $s){
					foreach ($to_grade as $g){
						// 查询是否已存在
						$row = $this->db->fetchRow("SELECT * FROM `tbl_score_tp` WHERE exam_id='{$e}' AND subject_id='{$s}' AND grade_id='{$g}' AND tp_type='{$tp_type}'");
						if (!empty($row)){
							$info = array(
							"tp_stat"        => $tp_stat,
							"tp_range"       => $tp_range,
							"tp_note" 		 => $tp_note,
							"update_time"    => date("Y-m-d H:i:s"),
							);
							$this->db->do_update("tbl_score_tp", $info, "tp_id='{$row['tp_id']}'");
						}else{

							$info = array(
							"grade_id"       => $g,
							"exam_id"        => $e,
							"subject_id"     => $s,
							"tp_type"        => $tp_type,
							"tp_stat"        => $tp_stat,
							"tp_range"       => $tp_range,
							"tp_note" 		 => $tp_note,
							"status"         => 1,
							"create_time"    => date("Y-m-d H:i:s"),
							"update_time"    => "0000-00-00 00:00:00",
							);
							$this->db->do_insert("tbl_score_tp", $info);
						}
					}
				}
			}

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=score.tplist&grade_id={$grade_id}&tp_type={$tp_type}");

		}


		// 年级列表
		$grade_lists = $this->model->grade->getAll();

		// 学科列表
		$subject_lists = $this->model->subject->getAll();

		// 年级下的考试列表
		$exam_lists = $this->model->exam->getListByGrade($grade_id);

		$this->tpl->assign("grade_id", $grade_id);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->assign("exam_lists", $exam_lists);
		$this->tpl->display("score/score.tpadd.tpl.php");
	}

	/** 成绩质量分析模板列表 **/
	public function actionTpList()
	{
		$grade_id = (int)$this->get->grade_id;
		$tp_type  = (int)$this->get->tp_type;

		if (empty($tp_type)){
			$tp_type = 1;
		}



		$where = " WHERE grade_id='{$grade_id}' AND tp_type='{$tp_type}' ";

		// 分页读取列表
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_score_tp` {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		// 列表
		$tp_lists = $this->db->fetchAll("SELECT * FROM `tbl_score_tp` {$where} ORDER BY tp_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		if (!empty($tp_lists)){
			$subject = $this->model->subject->getIni();
			$exam    = $this->model->exam->getIni();

			foreach ($tp_lists as $key => $value){
				$tp_lists[$key]['subject_name'] = $subject[$value['subject_id']];
				$tp_lists[$key]['exam_name']    = $exam[$value['exam_id']];

				// 统计项目
				$stat = @unserialize($value['tp_stat']);
				//var_dump($stat);
				$tmp = array();
				foreach ($stat as $k => $v){
					if (($v['show'] == 1)){
						if (!empty($v['max'])){
							$tmp[] = "{$k}({$v['min']}-{$v['max']})";
						}else{
							$tmp[] = "{$k}";
						}
					}
				}
				$tp_lists[$key]['stat_name'] = implode(",", $tmp);

				// 分析行名称
				$note = @unserialize($value['tp_note']);
				if(is_array($note)){
					$tp_lists[$key]['note_name'] = implode(",", $note);
				}

			}


		}
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));


		// 年级列表
		$grade_lists = $this->model->grade->getAll();

		//var_dump($tp_lists);
		$this->tpl->assign("grade_id", $grade_id);
		$this->tpl->assign("tp_type", $tp_type);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("tp_lists", $tp_lists);
		$this->tpl->display("score/score.tplist.tpl.php");
	}

	/** 删除质量分析模板 **/
	public function actionTpDel()
	{
		$tp_id = $this->get->tp_id;
		$grade_id = (int)$this->get->grade_id;
		$tp_type  = (int)$this->get->tp_type;

		if (empty($tp_type)){
			$tp_type = 1;
		}

		$this->db->query("DELETE  FROM `tbl_score_tp` WHERE tp_id='{$tp_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=score.tplist&grade_id={$grade_id}&tp_type={$tp_type}");
	}


	/** 成绩列表 **/
	public function actionList()
	{

		$class_id   = $this->get->class_id;
		$subject_id = $this->get->subject_id;
		$exam_id    = $this->get->exam_id;

		// 班级内所有学生
		$student_lists = $this->model->student->getInfoclass($class_id);
		foreach ($student_lists as $key => $value){
			$student[$value['student_id']] = $value;
		}
		$score_lists = $this->db->fetchAll("SELECT * FROM `tbl_score` WHERE class_id='{$class_id}' AND subject_id='{$subject_id}' AND exam_id='{$exam_id}' ");
		foreach ($score_lists as $key => $value){
			$score_lists[$key]['student_no']   = $student[$value['student_id']]['student_no'];
			$score_lists[$key]['student_name'] = $student[$value['student_id']]['student_name'];
		}


		// 图表数据
		$range_data = $this->db->fetchRow("SELECT range_level FROM `tbl_score_range` WHERE class_id='{$class_id}' AND subject_id='{$subject_id}' AND exam_id='{$exam_id}' ");
		$level = unserialize($range_data['range_level']);
		if (!empty($level)){
			foreach ($level as $key => $value){
				$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_score` WHERE class_id='{$class_id}' AND subject_id='{$subject_id}' AND exam_id='{$exam_id}' AND score_value>='{$value['min']}' AND  score_value<='{$value['max']}'");

				$level_lists["{$value['name']} ({$value['min']} - {$value['max']})"] = (int)$row['count'];
			}
		}


		$exam_name    = $this->model->exam->getNameById($exam_id);
		$class_name   = $this->model->class->getNameById($class_id);
		$subject_name = $this->model->subject->getNameById($subject_id);

		$this->tpl->assign("exam_name", $exam_name);
		$this->tpl->assign("class_name", $class_name);
		$this->tpl->assign("subject_name", $subject_name);
		$this->tpl->assign("student_lists", $student_lists);
		$this->tpl->assign("score_lists", $score_lists);
		$this->tpl->assign("level_lists", $level_lists);
		$this->tpl->display("score/score.list.tpl.php");
	}


	/** 分数修改 **/
	public function actionUpdate()
	{
		$score_id = (int)$this->get->score_id;

		$score_data = $this->model->score->getInfo($score_id);

		if ($_POST){
			$score_value = $this->post->score_value;

			$info = array(
			"score_value" => $score_value,
			"update_time" => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_score", $info, "score_id='{$score_id}'");

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=score.list&class_id={$score_data['class_id']}&exam_id={$score_data['exam_id']}&subject_id={$score_data['subject_id']}");

		}

		$score_data['exam_name']    = $this->model->exam->getNameById($score_data['exam_id']);
		$score_data['class_name']   = $this->model->class->getNameById($score_data['class_id']);
		$score_data['subject_name'] = $this->model->subject->getNameById($score_data['subject_id']);
		$score_data['student_name'] = $this->model->student->getNameById($score_data['student_id']);

		$this->tpl->assign("score_data", $score_data);
		$this->tpl->display("score/score.update.tpl.php");
	}

	/** 年级分数列表 **/
	public function actionGradeList()
	{
		$grade_id = $this->get->grade_id;
		$exam_id  = $this->get->exam_id;

		// 学期列表
		$semester_lists = $this->model->semester->getAll();

		// 年级列表
		$grade_lists = $this->model->grade->getAll();


		// 数据列表
		$class_lists = $this->model->class->getListByGrade($grade_id);
		foreach ($class_lists as $key => $value){
			// 各班按学科每人成绩分类
			$tmp = $this->db->fetchAll("SELECT * FROM `tbl_score` WHERE exam_id='{$exam_id}' AND class_id='{$value['class_id']}'");
			if (!empty($tmp)){
				foreach ($tmp as $k => $v){
					$data[$value['class_id']]['score'][$v['subject_id']][$v['student_id']] = $v['score_value'];
				}
				$data[$value['class_id']]['class_name'] = $value['class_name'];
			}
		}

		// 统计
		foreach ((array)$data as $key => $value){
			$total = 0;
			foreach ($value['score'] as $k => $v){
				$subject_name = $this->model->subject->getNameById($k);
				// 各科总分
				$tscore = array_sum($v);
				$data[$key]['tscore'][$subject_name] = $tscore;
				// 各科总分/各科总人数
				$vscore = intval($tscore/count($v));
				$data[$key]['vscore'][$subject_name] = $vscore;
				// 总平均分数
				$total += $vscore;
			}
			$data[$key]['total'] = $total;
			// 总分排名临时数组
			$sort[$key] += $total;

		}

		if (!empty($data)){
			// 排序 总分越高排名越前
			arsort($sort);
			$i = 0;
			foreach ($sort as $key => $value){
				$i++;
				$data_lists[$i] = $data[$key];
				$data_lists[$i]['class_rank'] = $i;
			}
		}


		$exam_name    = $this->model->exam->getNameById($exam_id);

		$this->tpl->assign("exam_name", $exam_name);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->display("score/score.gradelist.tpl.php");

	}

	/** 班级成绩列表 **/
	public function actionClassList()
	{
		$class_id = $this->get->class_id;
		$exam_id  = $this->get->exam_id;

		// 成绩数据
		$tmp = $this->db->fetchAll("SELECT * FROM `tbl_score` WHERE exam_id='{$exam_id}' AND class_id='{$class_id}'");
		if (!empty($tmp)){
			foreach ($tmp as $k => $v){
				$student_data = $this->model->student->getInfo($v['student_id']);

				$data[$v['student_id']]['student_no']   = $student_data['student_no'];
				$data[$v['student_id']]['student_name'] = $student_data['student_name'];
				$subject_name = $this->model->subject->getNameById($v['subject_id']);
				// 各科成绩
				$data[$v['student_id']]['score'][$subject_name] = $v['score_value'];
				// 总分
				$data[$v['student_id']]['total'] += $v['score_value'];

				// 总分排名临时数组
				$sort[$v['student_id']] += $v['score_value'];

			}
		}

		if (!empty($data)){
			// 排序 总分越高排名越前
			arsort($sort);
			$i = 0;
			foreach ($sort as $key => $value){
				$i++;
				$data_lists[$i] = $data[$key];
				$data_lists[$i]['class_rank'] = $i;
			}
		}

		// 学期列表
		$semester_lists = $this->model->semester->getAll();

		// 班级列表
		$class_lists = $this->model->class->getAll();

		$exam_name    = $this->model->exam->getNameById($exam_id);
		$class_name   = $this->model->class->getNameById($class_id);

		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("exam_name", $exam_name);
		$this->tpl->assign("class_id", $class_id);
		$this->tpl->assign("class_name", $class_name);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->display("score/score.classlist.tpl.php");
	}

	/** 学生成绩查询 - 分页**/
	public function actionStudentList()
	{
		$exam_id      = $this->get->exam_id;
		$student_name = $this->get->student_name;

		// 得到相似学生名称列表
		$student_lists = $this->db->fetchAll("SELECT student_id FROM `tbl_student` WHERE student_name LIKE '%{$student_name}%' ");
		if (!empty($student_lists)){
			foreach ($student_lists as $key => $value){
				$student_ids[] = "'".$value['student_id']."'";
			}

			$student_ids = implode(",", $student_ids);


			//  查询成绩
			$sql = "SELECT * FROM `tbl_score` WHERE exam_id='{$exam_id}' AND student_id IN ($student_ids) ";

			$tmp = $this->db->fetchAll($sql);
			if (!empty($tmp)){
				foreach ($tmp as $k => $v){
					$student_data = $this->model->student->getInfo($v['student_id']);

					$data[$v['student_id']]['student_no']   = $student_data['student_no'];
					$data[$v['student_id']]['student_name'] = $student_data['student_name'];
					$subject_name = $this->model->subject->getNameById($v['subject_id']);
					// 各科成绩
					$data[$v['student_id']]['score'][$subject_name] = $v['score_value'];
					// 总分
					$data[$v['student_id']]['total'] += $v['score_value'];

					// 总分排名临时数组
					$sort[$v['student_id']] += $v['score_value'];

				}
			}

			if (!empty($data)){
				// 排序 总分越高排名越前
				arsort($sort);
				$i = 0;
				foreach ($sort as $key => $value){
					$i++;
					$data_lists[$i] = $data[$key];
					$data_lists[$i]['class_rank'] = $i;
				}
			}


		}


		// 学期列表
		$semester_lists = $this->model->semester->getAll();
		$exam_name    = $this->model->exam->getNameById($exam_id);

		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("exam_name", $exam_name);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->display("score/score.studentlist.tpl.php");
	}


	//**-------------------------------------------- 学生 -----------------------------------------**//
	/** 分数列表 **/
	public function actionScoreList ()
	{
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_finance`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		// 学生
		$student_date = $this->db->fetchAll("SELECT `student_id`,`student_school`,`student_name` FROM `tbl_student` ORDER BY student_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		// 科目
		$subject_date = $this->db->fetchALL("SELECT `subject_name` FROM `tbl_subject` ");

		foreach($student_date as $key=>$value){

			// 取学科和成绩的字段
			$student_date[$key]['score_score'] = $this->db->fetchRow("SELECT * FROM `tbl_score` WHERE student_id='{$value['student_id']}'");
			// 反序列化
			$score_score = $student_date[$key]['score_score'];
			$score = unserialize($score_score['score_score']);

			foreach($subject_date as $k => $v){

				$student_date[$key]['score'][$k] = $score[$k]['score'];

			}
			// 求和


		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("score", $score);
		$this->tpl->assign("count", $count);
		$this->tpl->assign("score_date", $score_date);
		$this->tpl->assign("subject_date", $subject_date);
		$this->tpl->assign("student_date", $student_date);

		$this->tpl->display('score/score.list.tpl.php');

	}
	/** 分数添加 **/
	public function actionscoreAdd()
	{

		// 学科
		$subject_date = $this->db->fetchALL("SELECT `subject_id`,`subject_name` FROM `tbl_subject` ");

		if ($_POST){

			$score_score     	= $this->post->score_score;
			$subject_name     	= $this->post->subject_name;
			$student_name     	= $this->post->student_name;
			$student_school     = $this->post->student_school;


			// 科目变成二维数组
			$name = array();
			foreach($subject_name as $key=>$value){

				$name[]['name']=$value;
			}
			// 分数变成二维数组
			$score = array();
			foreach($score_score as $key=>$value){

				$score[]['score']=$value;
			}
			$cnt =count($subject_name);
			// 把两个数组 合在一起 用一个数组当健，另一个数组当值,将新数组序列化
			for($i=0;$i<$cnt;$i++){
				$score_serialization[$i]=array_merge($name[$i],$score[$i]);
			}
			$score_score = serialize($score_serialization);

			// 判断学生是否存在
			$student_date = $this->db->fetchROW("SELECT `student_id` FROM `tbl_student` WHERE student_name = '{$student_name}' AND student_school='{$student_school}'");

			if (empty($student_date)){
				Helper_Client::alert("学生不存在");
			}

			$student_id = $student_date['student_id'];
			// 写入数据库
			$info = array(

			"student_id"      		=> $student_id,
			"score_score"			=> $score_score,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_score", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=score.scorelist");
		}

		$this->tpl->assign("subject_date", $subject_date);
		$this->tpl->display('score/score.add.tpl.php');
	}
	/** 修改分数 **/
	public function actionscoreUpdate()
	{

		$student_id   = (int)$this->get->student_id;
		$score_data = $this->db->fetchROW("SELECT * FROM `tbl_score` WHERE `student_id`='{$student_id}'");
		// 学科
		$subject_list = $this->db->fetchALL("SELECT `subject_id`,`subject_name` FROM `tbl_subject` ");

		if (empty($score_data)){
			Helper_Client::alert("学生不存在");
		}
		if ($_POST){

			// 写入数据库
			$info = array(

			"student_id"      		=> $student_id,
			"score_score"      		=> $score_score,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_score", $info);
			$this->tpl->assign("score_lists", $score_lists);
			// 跳转
			//Helper_Client::location("添加成功", "index.php?a=score.scorelist");
		}

		$this->tpl->assign("score_data", $score_data);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->display('score/score.update.tpl.php');
	}
	/** 删除分数 **/
	public function actionscoreDel()
	{
		$score_id = $this->get->score_id;
		$this->db->query("DELETE  FROM `tbl_score` WHERE score_id='{$score_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=score.scorelist");
	}
	//**---------------------------------------- 成绩录入 -----------------------------------------**//
	/** 分数列表 **/
	public function actionScoreInThreeList ()
	{

		$this->tpl->display('score/scorein.three.list.tpl.php');

	}
	//新生成绩
	public function actionFirst(){
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_first_score`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$first_lists = $this->db->fetchAll("select * from `tbl_first_score` ORDER BY first_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign('first_lists',$first_lists);
		$this->tpl->display('score/first.tpl.php');
	}




	public function actionFirstIn(){

		if($_POST){
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];

			$check = $this->post->check;
			$data = $this->model->excel->in('first');

			if($check!=='on'){
				foreach ($data as $k => $v){
					$student_school = $v['2'];
					$row = $this->db->fetchRow("SELECT student_school FROM `tbl_first_score` WHERE student_school='{$student_school}' ");
					if (!empty($row)) {
						$mes = "学籍信息已存在，学籍号码为： ";
						$check = 2;
						$this->tpl->assign("mes", $mes);
						$this->tpl->assign("staff_card", $student_school);
						$this->tpl->assign("check", $check);
						$this->tpl->display("score/first.in.tpl.php");
						exit;
					}

				}
			}

			foreach ($data as $key => $value){

				if(empty($data)){
					Helper_Client::alert("请上传文件");
				}

				$info = array(
				"student_name" => $value[1],
				"student_school" => $value[2],
				"first_all" => $value[3],
				"first_yuwen" => $value[4],
				"first_math" => $value[5],
				"first_eng" => $value[6],//证件类型
				"first_phy" => $value[7],
				"first_chem" => $value[8],//date('Y-m-d',strtotime($value[8])),
				"first_his" => $value[9],
				"first_pol" => $value[10],
				"first_fix" => $value[11],
				"first_pe" => $value[12],
				"semester_id"    =>$semester_id
				);
				$student_school = $value[2];
				$row = $this->db->fetchRow("SELECT student_school FROM `tbl_first_score` WHERE student_school='{$student_school}'");
				if (!empty($row)) {
					$this->db->do_update("tbl_first_score", $info, "student_school='{$student_school}'" );
				}else{
					$this->db->do_insert("tbl_first_score", $info);
				}

			}
			//echo 123;exit;
			// 跳转
			Helper_Client::location("添加成功", "index.php?a=score.first");


		}

		$this->tpl->display("score/first.in.tpl.php");
	}
	public function actionFirstOut(){
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];
		//		$data_lists = $this->db->fetchAll("select * from `tbl_first_score` WHERE `semester_id` = {$semester_id} ORDER BY first_id DESC ");
		//
		//		foreach ($data_lists as $key => $value) {
		//
		//			$info = array(
		//			//编号	姓名	变动事项	日期	源单位	目的单位	在岗信息	备注	操作
		//
		//			"0" => $value['student_name'],
		//			"1" => $value['student_school'],
		//			"2" => $value['first_all'],
		//			"3" => $value['first_yuwen'],
		//			"4" => $value['first_math'],
		//			"5" => $value['first_eng'],
		//			"6" => $value['first_phy'],
		//			"7" => $value['first_chem'],
		//			"8" => $value['first_his'],
		//			"9" => $value['first_pol'],
		//			"10" => $value['first_fix'],
		//			"11" => $value['first_pe'],
		//
		//			);
		//			$data_lists[$key] = $info;
		//
		//		}
		$data_lists=array(array());
		$this->model->excel->outData("first", $data_lists);
	}

	//学生跟踪
	public function actionFolStudent(){
		$subject_lists = $this->db->fetchAll("select * from `tbl_subject` ORDER BY subject_id");

		$grade_lists = $this->model->grade->getall();
		foreach ($grade_lists as $key => $value){
			// 班级
			$row = $this->model->class->getListByGrade($value['grade_id']);
			foreach ($row as $k => $v){
				$grade_lists[$key]['class_name'][$v['class_id']] = $v['class_name'];
			}

		}

		if($this->get->class_id){

			$class_id		= (int)$this->get->class_id;
			// 年级名称
			$student_lists = $this->db->fetchAll("select * from `tbl_student` where class_id = '{$class_id}'");
		}
		$semester_lists = $this->model->semester->getAll();
		if(!empty($this->get->student_id)){
			if(!empty($this->get->student_id))$student_id = $this->get->student_id;
			if(!empty($this->get->semester_id1))$from = $this->get->semester_id1;
			if(!empty($this->get->semester_id2))$to = $this->get->semester_id2;
			if(!empty($this->get->exam_type))$exam_type =  $this->get->exam_type;
			//学生信息
			$student_data = $this->db->fetchRow("select * from `tbl_student` WHERE student_id = '{$student_id}'");


			$rank = '';
			if($this->get->rank_type==0){
				$rank = "and class_id = {$student_data['class_id']}";
			}

			if($exam_type=='all'){$exam_type='1,2,3,4';}
			//$exams = $this->db->fetchAll("select * from `tbl_exam` WHERE exam_type = {$exam_type}");
			$exam_lists = $this->db->fetchAll("select `exam_id` from `tbl_exam` WHERE
 `semester_id` >= '{$from}' AND `semester_id` <='{$to}' AND exam_type in ('{$exam_type}') ");
			//var_dump($exam_lists);

			$exams =array();
			foreach ($exam_lists as $key => $value){
				$exams[] = $value['exam_id'];
			}
			//        $exams = implode(',',$exams);//考试列表
			//        if(empty($exams)){
			//            $exams = 0;
			//        }
			//var_dump($exams);
			$all = array();//最外层  包装每一行数据
			foreach ($exams as $key => $value ){
				//再找tbl  score里该学生参与的考试  1获取学生信息
				//$sql = "SELECT a.*,b.semester_id,b.exam_type,(SELECT count(1) from tbl_score WHERE exam_id = {$value} AND subject_id = a.subject_id {$rank} AND score_value >a.score_value )+1 AS rank FROM `tbl_score` as a,`tbl_exam` as b WHERE a.exam_id = {$value} AND a.exam_id = b.exam_id AND a.student_id = ($student_id) ";
				//echo $sql;
				//          $score_lists = $this->db->fetchAll($sql);
				$sub_lists = array();
				$count = 0;
				//            foreach($score_lists as $k => $v){//把成绩按照学科做KEY
				//                $sub_lists[$v['subject_id']] = $v;
				//            }
				//            ksort($sub_lists);
				foreach ($subject_lists as $k => $v){
					$sql = "SELECT c.*,a.*,b.semester_id,b.exam_name,b.exam_type,(SELECT count(1) from tbl_score WHERE exam_id = {$value} AND subject_id = a.subject_id {$rank} AND score_value >a.score_value )+1 AS rank
              FROM `tbl_score` as a,`tbl_exam` as b ,tbl_semester as c WHERE b.semester_id=c.semester_id AND a.exam_id = {$value} AND a.exam_id = b.exam_id AND a.student_id = ($student_id) AND a.subject_id={$v['subject_id']}";
					$row1 = $this->db->fetchRow($sql);
					//var_dump($row);echo $row['semester_id'];exit;
					//                $sem = $row['semester_id'];
					//                $semester_data = $this->db->fetchRow("select * from `tbl_semester` WHERE `semester_id` = {$sem} ");
					//                $row['semester'] = $semester_data['semester_xname']."第".$semester_data['semester_name']."学期";
					//$exam_data = $this->db->fetchRow("select * from `tbl`")扔到外卖all
					$sub_lists[$v['subject_id']] = $row1;
					//var_dump( $sub_lists[$v['subject_id']]);
					if($row1['exam_name']){
						$all[$value]['exam'] = $row1['exam_name'];
					}
					if($row1['semester_xname']){
						$all[$value]['sem'] = $row1['semester_xname']."学年第".$row1['semester_name']."学期";
					}

				}

				if(empty($sub_lists))continue;
				//搞个总分 + 排名
				$sql = "SELECT sum(`score_value`) as count FROM tbl_score WHERE student_id = {$student_data['student_id']} AND exam_id = {$value}";
				//echo $sql;
				$row = $this->db->fetchRow($sql);
				$score = $row['count'];
				if(empty($score))continue;
				$sql = "SELECT count(1)+1 as rank FROM (SELECT sum(`score_value`) as count FROM tbl_score WHERE student_id in(SELECT student_id FROM `tbl_score` WHERE 1=1 {$rank}) GROUP BY student_id) as t WHERE count>'{$score}'";
				$row = $this->db->fetchRow($sql);
				$r = $row['rank'];
				$all[$value]['score'] = $score;
				$all[$value]['rank'] = $r;
				$all[$value]['sub'] = $sub_lists;

			}
			//print_r($all);
			$this->tpl->assign("student_data", $student_data);

		}
		// 获取年级id
		$row = $this->model->class->getinfo($class_id);
		$gradi_id = $row['grade_id'];
		//学科显示
		$this->tpl->assign("subject_lists",$subject_lists);
		$this->tpl->assign("all",$all);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("student_lists",$student_lists);

		$this->tpl->assign("class_id",$class_id);
		$this->tpl->assign("grade_id",$gradi_id);
		$this->tpl->assign("grade_lists",$grade_lists);
		$this->tpl->display("score/folstudent.tpl.php");
	}
	//年级跟踪
	public function actionFolGrade(){
		$subject_lists = $this->db->fetchAll("select * from `tbl_subject` ORDER BY subject_id");

		$grade_lists = $this->db->fetchAll("select * from `tbl_grade_up` WHERE is_now=1 ORDER BY grade_code ");


		if($this->get->grade_id){

			$grade_id		= (int)$this->get->grade_id;
			$grade_data = $this->db->fetchRow("SELECT * FROM `tbl_grade_up` WHERE is_now=1 AND grade_id='{$grade_id}'");
			$this->tpl->assign("gid",$grade_id);
			$this->tpl->assign("grade_data",$grade_data);
			// 年级名称
		}
		$semester_lists = $this->model->semester->getAll();
		if(!empty($this->get->gid)){//student_id为 级部id
			if(!empty($this->get->gid))$student_id = $this->get->gid;
			if(!empty($this->get->semester_id1))$from = $this->get->semester_id1;
			if(!empty($this->get->semester_id2))$to = $this->get->semester_id2;
			if(!empty($this->get->exam_type))$exam_type =  $this->get->exam_type;
			//该级部的班级   因为是和往届比较所以没有isnow
			//$student_data = $this->db->fetchRow("select * from `tbl_class_up` WHERE grade_id = '{$student_id}'");

			if($exam_type=='all'){$exam_type='1,2,3,4';}
			$exam_lists = $this->db->fetchAll("select * from `tbl_exam` WHERE
 `semester_id` >= {$from} AND `semester_id` <={$to} AND exam_type in ('{$exam_type}') AND grade_id = '{$student_id}'");

			//            $exams =array();
			//            foreach ($exam_lists as $key => $value){
			//                $exams[] = $value['exam_id'];
			//            }
			$all = array();//最外层  包装每一行数据
			//var_dump($exam_lists);
			foreach ($exam_lists as $key => $value ){

				$sub_lists = array();

				foreach ($subject_lists as $k => $v){
					//求各科平均分
					$sql = "SELECT avg(score_value) as mysum,subject_id from tbl_score WHERE exam_id = {$value['exam_id']} AND subject_id = {$v['subject_id']} ORDER BY subject_id";
					$row1 = $this->db->fetchRow($sql);
					$mysum = $row1['mysum'];
					//                    $row = array();
					//                    if($mysum){
					//                        $sql = "SELECT count(1)+1 as rank FROM (SELECT avg(score_value) as oa from tbl_score WHERE exam_id = {$value['exam_id']} AND subject_id = {$v['subject_id']} GROUP BY class_id) as a where oa>'{$mysum}'";
					//                        $row = $this->db->fetchRow($sql);
					//                    }
					//                    $row1 = array_merge($row1,$row);
					$sub_lists[$v['subject_id']] = $row1;


				}

				if(empty($sub_lists))continue;
				//总分
				$sql = "SELECT sum(myavg) as sum from(SELECT AVG(score_value) as myavg from tbl_score WHERE exam_id = {$value['exam_id']} GROUP BY subject_id) as t";
				$row = $this->db->fetchRow($sql);
				$score = $row['sum'];
				//                if(empty($score))continue;
				//                $sql = "select count(1) as rank from(select sum(myavg) as mysum from (SELECT AVG(score_value) as myavg,subject_id,class_id from tbl_score WHERE exam_id = {$value['exam_id']}  GROUP BY class_id,subject_id) as t GROUP BY class_id) as ta WHERE mysum>={$score}";
				//                $row = $this->db->fetchRow($sql);
				//                $r = $row['rank'];
				$all[$value['exam_id']]['score'] = $score;
				//$all[$value]['rank'] = $r;
				$all[$value['exam_id']]['sub'] = $sub_lists;
				if($row1=$this->db->fetchRow("select * from tbl_grade WHERE grade_id = {$value['grade_id']}")){
					$all[$value['exam_id']]['grade_name'] = $row1['grade_name'];
				}
				if($row1=$this->db->fetchRow("select * from tbl_exam WHERE exam_id = {$value['exam_id']}")){
					$all[$value['exam_id']]['exam'] = $row1['exam_name'];
				}

				if($row1['semester_id']){
					$row1 = $this->db->fetchRow("select * from tbl_semester WHERE semester_id={$row1['semester_id']}");
					$all[$value['exam_id']]['sem'] = $row1['semester_xname']."学年第".$row1['semester_name']."学期";
				}
			}
			//print_r($all);


		}

		// 获取年级id
		$row = $this->model->class->getinfo($class_id);
		$gradi_id = $row['grade_id'];
		//学科显示
		$this->tpl->assign("subject_lists",$subject_lists);


		$this->tpl->assign("all",$all);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("student_data",$student_data);
		$this->tpl->assign("grade_lists",$grade_lists);
		$this->tpl->display("score/folclass.tpl.php");
	}
	//班级跟踪
	public function actionFolClass(){//班级
		$subject_lists = $this->db->fetchAll("select * from `tbl_subject` ORDER BY subject_id");
		$class_id		= (int)$this->get->class_id;
		$grade_lists = $this->model->grade->getall();
		foreach ($grade_lists as $key => $value){
			// 班级
			$row = $this->model->class->getListByGrade($value['grade_id']);
			foreach ($row as $k => $v){
				$grade_lists[$key]['class_name'][$v['class_id']] = $v['class_name'];
			}

		}
		if($this->get->class_id){

			$class_id		= (int)$this->get->class_id;
			$this->tpl->assign("cid",$class_id);
			// 年级名称
		}
		$semester_lists = $this->model->semester->getAll();
		if(!empty($this->get->cid)){//student_id为 班级id
			if(!empty($this->get->cid))$student_id = $this->get->cid;
			if(!empty($this->get->semester_id1))$from = $this->get->semester_id1;
			if(!empty($this->get->semester_id2))$to = $this->get->semester_id2;
			if(!empty($this->get->exam_type))$exam_type =  $this->get->exam_type;
			//班级信息
			$student_data = $this->db->fetchRow("select * from `tbl_class_up` WHERE is_now=1 AND class_id = '{$student_id}'");

			if($exam_type=='all'){$exam_type='1,2,3,4';}
			$exam_lists = $this->db->fetchAll("select `exam_id` from `tbl_exam` WHERE
 `semester_id` >= {$from} AND `semester_id` <={$to} AND exam_type in ('{$exam_type}') ");

			$exams =array();
			foreach ($exam_lists as $key => $value){
				$exams[] = $value['exam_id'];
			}
			$all = array();//最外层  包装每一行数据
			foreach ($exams as $key => $value ){

				$sub_lists = array();

				foreach ($subject_lists as $k => $v){
					//求各科平均分
					$sql = "SELECT avg(score_value) as mysum,subject_id from tbl_score WHERE exam_id = {$value} AND subject_id = {$v['subject_id']} AND class_id = {$student_id} ORDER BY subject_id";
					$row1 = $this->db->fetchRow($sql);
					$mysum = $row1['mysum'];
					$row = array();
					if($mysum){
						$sql = "SELECT count(1)+1 as rank FROM (SELECT avg(score_value) as oa from tbl_score WHERE exam_id = {$value} AND subject_id = {$v['subject_id']} GROUP BY class_id) as a where oa>'{$mysum}'";
						$row = $this->db->fetchRow($sql);
					}
					$row1 = array_merge($row1,$row);
					$sub_lists[$v['subject_id']] = $row1;


				}

				if(empty($sub_lists))continue;
				//搞个总分 + 排名
				$sql = "SELECT sum(myavg) as sum from(SELECT AVG(score_value) as myavg from tbl_score WHERE exam_id = {$value} AND class_id = {$student_id} GROUP BY subject_id) as t";
				$row = $this->db->fetchRow($sql);
				$score = $row['sum'];
				if(empty($score))continue;
				$sql = "select count(1) as rank from(select sum(myavg) as mysum from (SELECT AVG(score_value) as myavg,subject_id,class_id from tbl_score WHERE exam_id = {$value}  GROUP BY class_id,subject_id) as t GROUP BY class_id) as ta WHERE mysum>={$score}";
				$row = $this->db->fetchRow($sql);
				$r = $row['rank'];
				$all[$value]['score'] = $score;
				$all[$value]['rank'] = $r;
				$all[$value]['sub'] = $sub_lists;
				if($row1=$this->db->fetchRow("select * from tbl_exam WHERE exam_id = {$value}")){
					$all[$value]['exam'] = $row1['exam_name'];
				}
				if($row1['semester_id']){
					$row1 = $this->db->fetchRow("select * from tbl_semester WHERE semester_id={$row1['semester_id']}");
					$all[$value]['sem'] = $row1['semester_xname']."学年第".$row1['semester_name']."学期";
				}
			}
			//print_r($all);


		}
		// 获取年级id

		$row = $this->model->class->getinfo($class_id);
		$grade_id = $row['grade_id'];

		//学科显示
		$this->tpl->assign("subject_lists",$subject_lists);


		$this->tpl->assign("all",$all);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("student_data",$student_data);
		$this->tpl->assign("grade_lists",$grade_lists);

		$this->tpl->assign("class_id",$class_id);
		$this->tpl->assign("grade_id",$grade_id);
		$this->tpl->display("score/folgrade.tpl.php");
	}
	//学科跟踪
	public function actionFolSubject(){
		$subject_lists = $this->db->fetchAll("select * from `tbl_subject` ORDER BY subject_id");

		$grade_lists = $this->model->grade->getall();
		$semester_lists = $this->model->semester->getAll();
		if($_POST){
			$subject_id = $this->post->subject_id;
			$_SESSION['folsub'] = $subject_id;
			$grade_id = $this->post->grade_id;
			$_SESSION['folgra'] = $grade_id;
		}
		if($this->get->exam_type){//提交表单了
			if($_SESSION['folsub']&&$_SESSION['folgra']){
				//            $wheresub = "subject_id = {$subject_id}";
				//            if($subject_id=='all'){
				//                $wheresub = "1=1";
				//            }
				$subject_id = $_SESSION['folsub'];
				$grade_id = $_SESSION['folgra'];
				if($grade_id){
					$grade_ids = implode(',',$grade_id);
				}
				$class_lists = $this->db->fetchAll("select * from tbl_class_up WHERE is_now=1 AND grade_id in ($grade_ids)");
				//            $class_ids = array();
				//            foreach ($class_lists as $key => $value){
				//                $class_ids[] = $value['class_id'];
				//            }
				//            if($class_ids){
				//                $class_ids = implode(',',$class_ids);
				//            }
				if(!empty($this->get->semester_id1))$from = $this->get->semester_id1;
				if(!empty($this->get->semester_id2))$to = $this->get->semester_id2;
				if(!empty($this->get->exam_type))$exam_type =  $this->get->exam_type;

				if($exam_type=='all'){$exam_type='1,2,3,4';}
				$exam_lists = $this->db->fetchAll("select `exam_id` from `tbl_exam` WHERE
 `semester_id` = {$to} AND exam_type in ('{$exam_type}') ");
				//一学期的成绩
				$exams =array();
				if(empty($exam_lists)){
					$exam_lists = array();
				}
				foreach ($exam_lists as $key => $value){
					$exams[] = $value['exam_id'];
				}

				$exams = implode(',',$exams);

				//12.2
				foreach ($class_lists as $key => $value ){
					//$sub_lists = array();
					if($subject_id=='all'){
						//搞个总分 + 排名
						$sql = "SELECT sum(myavg) as sum from(SELECT AVG(score_value) as myavg from tbl_score WHERE exam_id in ({$exams}) AND class_id = {$value['class_id']} GROUP BY subject_id) as t";
						$row = $this->db->fetchRow($sql);
						$score = $row['sum'];
						if(empty($score))continue;
						$sql = "select count(1)+1 as rank from(select sum(myavg) as mysum from (SELECT AVG(score_value) as myavg,subject_id,class_id from tbl_score WHERE exam_id in ('{$exams}')  GROUP BY class_id,subject_id) as t GROUP BY class_id) as ta WHERE mysum>{$score}";
						//echo $sql;
						$row = $this->db->fetchRow($sql);
						$r = $row['rank'];
						$class_lists[$key]['score'] = $score;
						$class_lists[$key]['rank'] = $r;
					}else{

						//求科平均分
						$sql = "SELECT avg(score_value) as mysum,subject_id from tbl_score WHERE exam_id in ('{$exams}') AND subject_id = {$subject_id} AND class_id = {$value['class_id']} ORDER BY subject_id";
						//echo $sql;
						$row1 = $this->db->fetchRow($sql);
						$mysum = $row1['mysum'];
						$class_lists[$key]['score'] = $mysum;
						$row = array();
						if($mysum){
							$sql = "SELECT count(1)+1 as rank FROM (SELECT avg(score_value) as oa from tbl_score WHERE exam_id in ('{$exams}') AND subject_id = {$subject_id} GROUP BY class_id) as a where oa>'{$mysum}'";
							$row = $this->db->fetchRow($sql);
						}
						$class_lists[$key]['rank'] = $row['rank'];
						$row = $this->db->fetchRow("select * from tbl_staff_subject where semester_id = {$to} AND subject_id = {$subject_id}  AND class_id = '{$class_lists[$key]['class_id']}'");
						$row2 = $this->db->fetchRow("select staff_name from tbl_staff WHERE staff_id = '{$row['staff_id']}'");
						$class_lists[$key]['teacher'] = $row2['staff_name'];

					}//else


				}//class_lists foreach


				//另一学期的成绩
				if($exam_type=='all'){$exam_type='1,2,3,4';}
				$exam_lists = $this->db->fetchAll("select `exam_id` from `tbl_exam` WHERE
 `semester_id` = {$from} AND exam_type in ('{$exam_type}') ");

				$exams =array();
				if(empty($exam_lists)){
					$exam_lists = array();
				}
				foreach ($exam_lists as $key => $value){
					$exams[] = $value['exam_id'];
				}

				$exams = implode(',',$exams);

				//12.2
				foreach ($class_lists as $key => $value ){
					//$sub_lists = array();
					if($subject_id=='all'){
						//搞个总分 + 排名
						$sql = "SELECT sum(myavg) as sum from(SELECT AVG(score_value) as myavg from tbl_score WHERE exam_id in ({$exams}) AND class_id = {$value['class_id']} GROUP BY subject_id) as t";
						$row = $this->db->fetchRow($sql);
						$score = $row['sum'];
						if(empty($score))continue;
						$sql = "select count(1)+1 as rank from(select sum(myavg) as mysum from (SELECT AVG(score_value) as myavg,subject_id,class_id from tbl_score WHERE exam_id in ('{$exams}')  GROUP BY class_id,subject_id) as t GROUP BY class_id) as ta WHERE mysum>{$score}";
						//echo $sql;
						$row = $this->db->fetchRow($sql);
						$r = $row['rank'];
						$class_lists[$key]['score'] = $score;
						$class_lists[$key]['rank'] = $r;
					}else{

						//求科平均分
						$sql = "SELECT avg(score_value) as mysum,subject_id from tbl_score WHERE exam_id in ('{$exams}') AND subject_id = {$subject_id} AND class_id = {$value['class_id']} ORDER BY subject_id";
						//echo $sql;
						$row1 = $this->db->fetchRow($sql);
						$mysum = $row1['mysum'];
						$class_lists[$key]['score2'] = $mysum;
						$row = array();
						if($mysum){
							$sql = "SELECT count(1)+1 as rank FROM (SELECT avg(score_value) as oa from tbl_score WHERE exam_id in ('{$exams}') AND subject_id = {$subject_id} GROUP BY class_id) as a where oa>'{$mysum}'";
							$row = $this->db->fetchRow($sql);
						}
						$class_lists[$key]['rank2'] = $row['rank'];
						$row = $this->db->fetchRow("select * from tbl_staff_subject where semester_id = {$to} AND subject_id = {$subject_id} AND class_id = '{$class_lists[$key]['class_id']}'");
						$row2 = $this->db->fetchRow("select staff_name from tbl_staff WHERE staff_id = '{$row['staff_id']}'");
						$class_lists[$key]['teacher2'] = $row2['staff_name'];


					}//else


				}//class_lists foreach
				//var_dump($class_lists);
				$this->tpl->assign("class_lists",$class_lists);
				//12.2
				if($subject_id=='all'){
					$this->tpl->assign('subject_name','总分');
				}else{
					$row = $this->db->fetchRow("select subject_name from `tbl_subject` WHERE subject_id ='{$subject_id}'");
					$this->tpl->assign('subject_name',$row['subject_name']);
				}
			}else{
				Helper_Client::alert("请先选择学科和年级");
			}
		}


		$this->tpl->assign("semester_lists",$semester_lists);
		$this->tpl->assign("grade_lists",$grade_lists);
		$this->tpl->assign("subject_lists",$subject_lists);
		$this->tpl->display("score/folsubject.tpl.php");
	}
	//数据统计
	public function actionData(){
		$grade_lists = $this->db->fetchAll("select * from `tbl_grade_up` WHERE is_now =1");
		$semester_lists = $this->model->semester->getAll();
		$subject_lists = $this->db->fetchAll("select * from `tbl_subject` ORDER BY subject_id");
		//var_dump($_GET);
		if($this->get->nian&&$this->get->exam_id){
			$classes = implode(',',$this->get->nian);
			if(!empty($classes)){
				switch ($this->get->student_type){
					case 0:
						$stype = "1=1";
						break;
					case 1:
						$stype = "b.student_classes != 1";
						break;
					case 2:
						$stype = "b.student_variation not like %转入%";
						break;
					case 3:
						$stype = "b.student_classes = 1";
						break;
					case 4:
						$stype = "b.student_variation like %转入%";
						break;
				}
				$class_lists = $this->db->fetchAll("select * from tbl_class_up WHERE class_id in ($classes)");
				$class_lists2 = $class_lists;

				if($this->get->subject_id=='all'){
					$where = '1=1';
					$rangesub = ' score_end = 1 ';
				}else{
					$where = "a.subject_id = '{$this->get->subject_id}'";
					$rangesub = " subject_id = '{$this->get->subject_id}' ";
				}
				foreach ($class_lists as $key => $value){
					//该班级的报表分数段
					$range = $this->db->fetchRow("SELECT * FROM `tbl_score_range` WHERE class_id = '{$value['class_id']}' AND exam_id = '{$this->get->exam_id}' AND {$rangesub}");
					$range['range_level'] = unserialize($range['range_level']);
					$range['range_section'] = unserialize($range['range_section']);
					//var_dump($range['range_section'] );
					if(is_array($range['range_section'])){
						foreach ($range['range_section'] as $k => $v) {

							switch ($k) {
								case 1:
									$range['range_section'][$k]['name'] = '优秀';
									break;
								case 2:
									$range['range_section'][$k]['name'] = '良好';
									break;
								case 3:
									$range['range_section'][$k]['name'] = '优良';
									break;
								case 4:
									$range['range_section'][$k]['name'] = '合格';
									break;
								case 5:
									$range['range_section'][$k]['name'] = '及格';
									break;
								case 6:
									$range['range_section'][$k]['name'] = '不及格';
									break;
								case 7:
									$range['range_section'][$k]['name'] = '极差';
									break;
							}
							if($v['open']!='1') unset($range['range_section'][$k]);
						}
					}

					$this->tpl->assign("r_level",$range['range_level']);
					$this->tpl->assign("r_section",$range['range_section']);

					//参考人数
					$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND {$stype}");
					$class_lists[$key]['count'] = $row['count'];

					//分阶内人数
					if(is_array($range['range_level'])){
						foreach ($range['range_level'] as $k => $v){
							$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND {$stype} AND score_value<'{$v['max']}' AND score_value >='{$v['min']}'");
							$class_lists[$key]['level'][$v['name']] = $row['count'];
						}

					}

					//优秀率
					if($class_lists[$key]['count']>0){
						foreach ($range['range_section'] as $k => $v){
							//if($v['open']!='1') continue;

							$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND {$stype} AND score_value<'{$v['max']}' AND score_value >='{$v['min']}'");
							$class_lists[$key]['section'][$v['name']] = $row['count']/$class_lists[$key]['count']*100;;
						}
					}

					//高
					$row = $this->db->fetchRow("select max(score_value) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND {$stype}");
					$class_lists[$key]['max'] = $row['count'];
					//低
					$row = $this->db->fetchRow("select min(score_value) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND {$stype}");
					$class_lists[$key]['min'] = $row['count'];
					//平均
					$row = $this->db->fetchRow("select avg(score_value) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND {$stype}");
					$class_lists[$key]['avg'] = $myavg = $row['count'];
					//rank
					$row = $this->db->fetchRow("select count(1) as count from(select avg(score_value) as count1,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND {$where} AND {$stype} GROUP BY a.class_id) as t WHERE count1>'{$myavg}'");
					$class_lists[$key]['rank'] = $row['count'];
					$row = $this->db->fetchRow("select count(1) as count from tbl_student as b WHERE b.class_id = '{$value['class_id']}' AND {$stype}");
					$class_lists[$key]['all'] = $row['count'];
					$row = $this->db->fetchRow("select * from tbl_staff_subject where subject_id = '{$this->get->subject_id}' AND class_id = '{$value['class_id']}' ORDER BY id DESC ");
					$row2 = $this->db->fetchRow("select staff_name from tbl_staff WHERE staff_id = '{$row['staff_id']}'");
					$class_lists[$key]['teacher'] = $row2['staff_name'];
				}//classlist foreach
				//var_dump($class_lists);
				foreach ($class_lists2 as $key => $value){
					//高
					$row = $this->db->fetchRow("select  b.* from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND {$stype} ORDER BY score_value DESC ");
					$class_lists2[$key]['max'] = array($row);
					//低
					$row = $this->db->fetchRow("select b.* from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND {$stype} ORDER BY score_value ASC ");
					$class_lists2[$key]['min'] = array($row);
					//60
					$row = $this->db->fetchAll("select b.* from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}' AND a.class_id = '{$value['class_id']}' AND {$where} AND {$stype} AND score_value<60");

					$class_lists2[$key]['bad'] = $row;

				}
				$this->tpl->assign("class_lists",$class_lists);
				$this->tpl->assign("class_lists2",$class_lists2);
				$exam = $this->get->exam_id;
				$subject = $this->get->subject_id;
				$grade = $this->get->grade_id;
				$semester_id = $this->get->semester_id;
				if($subject=='all'){
					$subject_name = '总分';
				}else{
					$subject_name = $this->model->subject->getNameById($subject);
				}
				if($grade){
					$grade_name = $this->model->grade->getNameById($grade);
				}
				if($semester_id){
					$semester_name = $this->model->semester->getNameById($semester_id);
				}
				if($exam){
					$exam_name = $this->model->exam->getNameById($exam);
				}
				$this->tpl->assign("semester_name",$semester_name);
				$this->tpl->assign("grade_name",$grade_name);
				$this->tpl->assign("exam_name",$exam_name);
				$this->tpl->assign("subject_name",$subject_name);
			}//class存在
			//print_r($class_lists2);
		}else{
			//Helper_Client::alert("请先选择左侧菜单项");
		}

		$this->tpl->assign("subject_lists",$subject_lists);
		$this->tpl->assign("semester_lists",$semester_lists);
		$this->tpl->assign("grade_lists",$grade_lists);
		$this->tpl->display("score/data.tpl.php");
	}
	public function actionSemJson ()
	{
		$sem_id = (int)$this->get->sem_id;
		if (!empty($sem_id)) {
			$grade_lists = $this->db->fetchAll("select * from tbl_exam WHERE semester_id = {$sem_id}");

		}

		if(!empty($grade_lists)){
			echo json_encode($grade_lists);
		}else{
			exit("0");
		}
	}
	public function actionSemJson2 ()
	{
		$sem_id = (int)$this->get->sem_id;
		if (!empty($sem_id)) {
			$grade_lists = $this->db->fetchAll("select * from tbl_exam WHERE exam_type=4 AND semester_id = {$sem_id}");

		}

		if(!empty($grade_lists)){
			echo json_encode($grade_lists);
		}else{
			exit("0");
		}
	}
	//质量分析表格
	public function actionAnalyze(){
		$grade_lists = $this->db->fetchAll("select * from `tbl_grade_up` WHERE is_now =1");
		$semester_lists = $this->model->semester->getAll();
		$subject_lists = $this->db->fetchAll("select * from `tbl_subject` ORDER BY subject_id");

		$tptype = $this->get->tptype;
		//		$tp = $this->db->fetchRow("select * from tbl_score_tp WHERE tp_id=5");// grade_id = '{$this->get->grade_id}' AND subject_id ='{$this->get->subject_id}'
		//		$stat = $tp['tp_stat'] = unserialize($tp['tp_stat']);
		//		foreach ($tp['tp_stat'] as $k => $v){
		//			if($v['show']!='1')unset($tp['tp_stat'][$k]);
		//		}
		//		$tp['tp_range'] = unserialize($tp['tp_range']);
		//		$tp['tp_note'] = unserialize($tp['tp_note']);
		//		$_SESSION['anlyze_tp'] = serialize($tp);
		if($this->get->nian&&$this->get->exam_id&&$this->get->subject_id){

			if($tptype==2){
				$tpwhere = " AND tp_type = 2 ";
			}else{
				$tpwhere = " AND tp_type = 1 ";
			}
			$classes = implode(',',$this->get->nian);
			if(!empty($classes)){
				//先找寻是否有历史记录
				$note = serialize($_GET);
				$history = $this->db->fetchRow("SELECT * FROM `tbl_anlyze` WHERE anlyze_id=2");
				if($history){
					$this->tpl->assign("history",unserialize($history['anlyze_note']));
				}
				//历史记录结束
				$class_lists = $this->db->fetchAll("select * from tbl_class_up WHERE class_id in ($classes)");
				$tp = $this->db->fetchRow("select * from tbl_score_tp WHERE grade_id = '{$this->get->grade_id}' AND subject_id ='{$this->get->subject_id}' AND exam_id = '{$this->get->exam_id}' {$tpwhere} ORDER BY create_time DESC ");//
				//$tp = $this->db->fetchRow("SELECT * FROM tbl_score_tp WHERE tp_id=27");
				if(empty($tp)){Helper_Client::alert("请先在质量分析表模设置");exit;}
				$tp_range = unserialize($tp['tp_range']);

				$stat = $tp['tp_stat'] = unserialize($tp['tp_stat']);
				//var_dump($stat['优秀分段']);
				$this->tpl->assign("youxiu",$stat['优秀分段']);
				$this->tpl->assign("jige",$stat['合格分段']);
				foreach ($tp['tp_stat'] as $k => $v){
					if($v['show']!='1')unset($tp['tp_stat'][$k]);
					if($k=='优秀分段')unset($tp['tp_stat'][$k]);
					if($k=='良好分段')unset($tp['tp_stat'][$k]);
					if($k=='合格分段')unset($tp['tp_stat'][$k]);
					if($k=='极差分段')unset($tp['tp_stat'][$k]);
					if($k=='班级')unset($tp['tp_stat'][$k]);
				}
				$tp['tp_range'] = unserialize($tp['tp_range']);
				$tp['tp_note'] = unserialize($tp['tp_note']);

				$this->tpl->assign("tp",$tp);

				if($this->get->subject_id=='all'){
					$where = '1=1';
				}else{
					$where = "a.subject_id = '{$this->get->subject_id}'";
				}
				$all = array();
				$aa = array();
				//年级与考人数  用于后面计算
				$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
   AND {$where}");
				$examcount2 = $row['count'];
				foreach ($class_lists as $key => $value){
					//与考人数  用于后面计算
					$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where}");
					$examcount = $row['count'];
					//班级
					//if(key_exists("班级",$tp['tp_stat'])){
					$all[$key]['class_name'] = $value['class_name'];
					//期初人数
					if(key_exists("期初人数",$tp['tp_stat'])){
						$row = $this->db->fetchRow("select count(1) as count from tbl_student WHERE class_id = '{$value['class_id']}'");
						$all[$key]['all'] = $row['count'];
						$aa['all'] += $row['count'];
					}
					//}//参考人数
					if(key_exists("与考人数",$tp['tp_stat'])){;
					$all[$key]['count'] = $examcount;
					$aa['count'] += $examcount;
					}
					//与考率
					if(key_exists("与考率",$tp['tp_stat'])){
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where}");
						$row1 = $this->db->fetchRow("select count(1) as count from tbl_student as b WHERE b.class_id = '{$value['class_id']}'");
						if($row1['count']==0){
							$all[$key]['countrate'] = "";
						}else{
							$all[$key]['countrate'] = $row['count']*100/$row1['count'];
						}

						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND  {$where}");
						$row1 = $this->db->fetchRow("select count(1) as count from tbl_student as b WHERE b.grade_id = '{$value['grade_id']}'");
						if($row1['count']==0){
							$aa['countrate'] = "";
						}else{
							$aa['countrate'] = $row['count']*100/$row1['count'];
						}

					}//总分
					if(key_exists("总分",$tp['tp_stat'])){
						$row = $this->db->fetchRow("select sum(score_value) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} ");
						$all[$key]['sum'] = $row['count'];
						$aa['sum'] = $row['count'];
					}//平均分
					if(key_exists("平均分",$tp['tp_stat'])){
						$row = $this->db->fetchRow("select avg(score_value) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} ");
						$all[$key]['avg'] = $row['count'];
						$aa['avg'] = $row['count'];
					}//	最高分
					if(key_exists("最高分",$tp['tp_stat'])){
						$row = $this->db->fetchRow("select max(score_value) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} ");
						$all[$key]['max'] = $row['count'];
						$aa['max'] = $row['count'];
					}//最低分
					if(key_exists("最低分",$tp['tp_stat'])){
						$row = $this->db->fetchRow("select min(score_value) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} ");
						$all[$key]['max'] = $row['count'];
						$aa['min'] = $row['count'];
					}//优秀人数
					if(key_exists("优秀人数",$tp['tp_stat'])){
						//此处分值在$stat['优秀人数']['max']中
						$max = $stat['优秀分段']['max'];
						$min = $stat['优秀分段']['min'];
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						$all[$key]['bettercount'] = $row['count'];
						$aa['bettercount'] += $row['count'];
					}
					if(key_exists("优秀率",$tp['tp_stat'])){
						//此处分值在
						$max = $stat['优秀分段']['max'];
						$min = $stat['优秀分段']['min'];
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						if($examcount!=0){$all[$key]['betterrate'] = @$row['count']*100/$examcount;
						}else{
							$all[$key]['betterrate'] ='';
						}
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						if($examcount!=0){$aa['betterrate'] = @$row['count']*100/$examcount2;
						}else{
							$aa['betterrate'] ='';
						}
					}//良好人数
					if(key_exists("良好人数",$tp['tp_stat'])){
						//此处分值在$stat['优秀人数']['max']中
						$max = $stat['良好分段']['max'];
						$min = $stat['良好分段']['min'];
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						$all[$key]['goodcount'] = $row['count'];
						$aa['goodcount'] = $row['count'];
					}
					if(key_exists("良好率",$tp['tp_stat'])){
						//此处分值在$stat['优秀人数']['max']中
						$max = $stat['良好分段']['max'];
						$min = $stat['良好分段']['min'];
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						if($examcount!=0){$all[$key]['goodrate'] = @$row['count']*100/$examcount;
						}else{
							$all[$key]['goodrate'] ='';
						}
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						if($examcount!=0){$aa['goodrate'] = @$row['count']*100/$examcount2;
						}else{
							$aa['goodrate'] ='';
						}
					}
					if(key_exists("合格人数",$tp['tp_stat'])){
						//此处分值在$stat['优秀人数']['max']中
						$max = $stat['合格分段']['max'];
						$min = $stat['合格分段']['min'];
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						$all[$key]['okcount'] = $row['count'];
						$aa['okcount'] = $row['count'];
					}
					if(key_exists("合格率",$tp['tp_stat'])){
						//此处分值在$stat['优秀人数']['max']中
						$max = $stat['合格分段']['max'];
						$min = $stat['合格分段']['min'];
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						if($examcount!=0){$all[$key]['okrate'] = @$row['count']*100/$examcount;
						}else{
							$all[$key]['okrate'] ='';
						}
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						if($examcount!=0){$aa['okrate'] = @$row['count']*100/$examcount2;
						}else{
							$aa['okrate'] ='';
						}
					}
					if(key_exists("极差人数",$tp['tp_stat'])){
						//此处分值在$stat['优秀人数']['max']中
						$max = $stat['极差分段']['max'];
						$min = $stat['极差分段']['min'];
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						$all[$key]['worsecount'] = $row['count'];
						$aa['worsecount'] = $row['count'];
					}
					if(key_exists("极差率",$tp['tp_stat'])){
						//此处分值在$stat['优秀人数']['max']中
						$max = $stat['极差分段']['max'];
						$min = $stat['极差分段']['min'];
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						if($examcount!=0){$all[$key]['worserate'] = @$row['count']*100/$examcount;
						}else{
							$all[$key]['worserate'] ='';
						}
						$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
  AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>='{$min}' AND score_value<'{$max}'");
						if($examcount!=0){$aa['worserate'] = @$row['count']*100/$examcount2;
						}else{
							$aa['worserate'] ='';
						}
					}
					//分段部分
					if(is_array($tp['tp_range'])) {
						foreach ($tp['tp_range'] as $k => $v) {
							$row = $this->db->fetchRow("select count(1) as count,b.student_classes,b.student_variation from tbl_score as a ,tbl_student as b WHERE a.student_id=b.student_id AND a.exam_id = '{$this->get->exam_id}'
                            AND a.class_id = '{$value['class_id']}' AND {$where} AND score_value>{$v['min']} AND score_value<={$v['max']}");
							$all[$key]['range' . $k] = $row['count'];
							$aa['range' . $k] += $row['count'];
						}
					}
				}//classlist foreach
				$_SESSION['anlyze_get'] = serialize($_GET);
				$_SESSION['anlyze_content'] =serialize($all);
				$_SESSION['anlyze_content_all'] =serialize($aa);
				if($this->get->subject_id=='all'){
					$subject_name = '总分';
				}else{
					$subject_name = $this->model->subject->getNameById($this->get->subject_id);
				}
				if($this->get->grade_id){
					$grade_name = $this->model->grade->getNameById($this->get->grade_id);
				}

				$this->tpl->assign("subject_name",$subject_name);
				$this->tpl->assign("grade_name",$grade_name);
				$this->tpl->assign("all",$all);
				$this->tpl->assign("aa",$aa);

			}//class存在
			//print_r($class_lists2);

		}else{
			//Helper_Client::alert("请先选择左侧菜单项");
		}
		if($_POST){
			if($_SESSION['anlyze_content']&&$_SESSION['anlyze_content_all']&&$_SESSION['anlyze_tp']){
				$_note = serialize($this->post->note);
				$info = array(
				'anlyze_content'    => $_SESSION['anlyze_content'],
				'anlyze_content_all'    => $_SESSION['anlyze_content_all'],
				'create_time'  =>date("Y-m-d H:i:s"),
				'anlyze_tp'   =>$_SESSION['anlyze_tp'],
				'anlyze_get'  =>$_SESSION['anlyze_get'],
				'anlyze_note'  =>$_note,
				);
				$this->db->do_insert("tbl_anlyze", $info);
				Helper_Client::alert("保存成功");
			}else{
				Helper_Client::alert("会话过期请重新查询");
			}
		}
		$username = $this->model->user->getNameById($this->user['user_id']);
		$this->tpl->assign("tptype",$tptype);
		$this->tpl->assign("school",$this->system_ini);
		$this->tpl->assign("username",$username);
		$now = date("Y-m-d",time());
		$this->tpl->assign("now",$now);
		$this->tpl->assign("subject_lists",$subject_lists);
		$this->tpl->assign("semester_lists",$semester_lists);
		$this->tpl->assign("grade_lists",$grade_lists);
		$this->tpl->display("score/analyze.tpl.php");
	}

	public function actiongoodbad(){
		$grade_lists = $this->db->fetchAll("select * from `tbl_grade_up` WHERE is_now =1");
		$semester_lists = $this->model->semester->getAll();
		$subject_lists = $this->db->fetchAll("select * from `tbl_subject` ORDER BY subject_id");
		if($this->get->nian&&$this->get->exam_id&&$this->get->subject_id){
			$exam = $this->get->exam_id;
			$subject = $this->get->subject_id;
			$classes = implode(',',$this->get->nian);
			$grade = $this->get->grade_id;
			$semester_id = $this->get->semester_id;
			$this->tpl->assign("semester",$semester_id);
			$this->tpl->assign("grade",$grade);
			$this->tpl->assign("exam",$exam);
			$this->tpl->assign("subject",$subject);
			$this->tpl->assign("classes",$classes);
			if($subject=='all'){
				$subject_name = '总分';
			}else{
				$subject_name = $this->model->subject->getNameById($subject);
			}
			if($grade){
				$grade_name = $this->model->grade->getNameById($grade);
			}
			if($semester_id){
				$semester_name = $this->model->semester->getNameById($semester_id);
			}
			if($exam){
				$exam_name = $this->model->exam->getNameById($exam);
			}
			$this->tpl->assign("semester_name",$semester_name);
			$this->tpl->assign("grade_name",$grade_name);
			$this->tpl->assign("exam_name",$exam_name);
			$this->tpl->assign("subject_name",$subject_name);
		}
		if($_POST){
			$good = $this->post->good;
			$bad = $this->post->bad;
			if(empty($good)&&empty($bad)) {
				Helper_Client::alert("请输入绩优生或绩差生");exit;
			}
			if(empty($this->post->exam_id) or empty($this->post->subject_id) or empty($this->post->nian) ) {
				Helper_Client::alert("请确保左侧选择完毕");exit;
			}
			$semester_id = $this->post->semester_id;
			$exam = $this->post->exam_id;
			$subject = $this->post->subject_id;
			$classes = $this->post->nian;
			$grade = $this->post->grade_id;

			if($good){
				$order = "order by mysum desc";// limit '{$good}'
				$limit = $good;
				$this->tpl->assign("limit","前".$limit);
			}else{
				$order = "order by mysum";// limit '{$bad}'
				$limit = $bad;
				$this->tpl->assign("limit","后".$limit);
			}
			//获取前一次同类型考试
			$exam_data = $this->db->fetchRow("select * from `tbl_exam` WHERE exam_id='{$exam}'");
			$exam_data2 = $this->db->fetchRow("select * from `tbl_exam` WHERE create_time<'{$exam_data['create_time']}' AND grade_id = '{$exam_data['grade_id']}' AND exam_type='{$exam_data['exam_type']}'");
			$exam2 = $exam_data2['exam_id'];
			if(empty($exam2)) $exam2 = $exam;
			if($subject=='all'){
				$score_lists = $this->db->fetchAll("SELECT a.*,sum(a.score_value) as mysum,sum(b.score_value) as mysum2 FROM `tbl_score` as a LEFT JOIN `tbl_score` as b ON a.student_id = b.student_id AND a.subject_id = b.subject_id WHERE b.exam_id='{$exam2}' AND a.exam_id ='{$exam}' AND a.class_id in ($classes) GROUP BY a.student_id {$order}");
			}else{
				$where = "a.subject_id = '{$subject}'";
				$score_lists = $this->db->fetchAll("SELECT a.*,a.score_value as mysum,b.score_value as mysum2 FROM `tbl_score` as a LEFT JOIN `tbl_score` as b ON a.student_id = b.student_id AND a.subject_id = b.subject_id WHERE {$where} AND b.exam_id='{$exam2}' AND a.exam_id ='{$exam}' AND a.class_id in ($classes) GROUP BY a.student_id {$order}");
			}
			foreach ($score_lists as $key => $value){
				$row = $this->model->student->getInfo($value['student_id']);
				$classname = $this->model->class->getNameById($row['class_id']);
				$score_lists[$key]['classname'] = $classname;
				$score_lists[$key]['seat'] = $row['student_seat'];
				$score_lists[$key]['student_name'] = $row['student_name'];
				$rank = 1;$rank2 = 1;
				foreach ($score_lists as $k => $v){
					if($v['mysum']>$value['mysum']) $rank++;
					if($v['mysum2']>$value['mysum2']) $rank2++;
				}
				$score_lists[$key]['rank'] =$rank;
				$score_lists[$key]['rank2'] =$rank2;
			}//score_lists
			if($limit>0){
				array_splice($score_lists,$limit);
			}
			$this->tpl->assign("score_lists",$score_lists);
			//var_dump($score_lists);
		}
		if($subject=='all'){
			$subject_name = '总分';
		}else{
			$subject_name = $this->model->subject->getNameById($subject);
		}
		if($grade){
			$grade_name = $this->model->grade->getNameById($grade);
		}
		if($semester_id){
			$semester_name = $this->model->semester->getNameById($semester_id);
		}
		if($exam){
			$exam_name = $this->model->exam->getNameById($exam);
		}
		$this->tpl->assign("semester_name",$semester_name);
		$this->tpl->assign("grade_name",$grade_name);
		$this->tpl->assign("exam_name",$exam_name);
		$this->tpl->assign("subject_name",$subject_name);

		$this->tpl->assign("subject_lists",$subject_lists);
		$this->tpl->assign("semester_lists",$semester_lists);
		$this->tpl->assign("grade_lists",$grade_lists);
		$this->tpl->display("score/goodbad.tpl.php");
	}
}
?>